home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / MenuComponent.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  9.9 KB  |  339 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)MenuComponent.java    1.43 98/08/31
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.MenuComponentPeer;
  17. import java.awt.event.ActionEvent;
  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import sun.awt.AppContext;
  21. import sun.awt.SunToolkit;
  22.  
  23. /**
  24.  * The abstract class <code>MenuComponent</code> is the superclass 
  25.  * of all menu-related components. In this respect, the class
  26.  * <code>MenuComponent</code> is analogous to the abstract superclass
  27.  * <code>Component</code> for AWT components.
  28.  * <p>
  29.  * Menu components receive and process AWT events, just as components do,
  30.  * through the method <code>processEvent</code>.
  31.  *
  32.  * @version     1.43, 08/31/98
  33.  * @author     Arthur van Hoff
  34.  * @since       JDK1.0
  35.  */
  36. public abstract class MenuComponent implements java.io.Serializable {
  37.  
  38.     static {
  39.         /* ensure that the necessary native libraries are loaded */
  40.     Toolkit.loadLibraries();
  41.         initIDs();
  42.     }
  43.  
  44.     transient MenuComponentPeer peer;
  45.     transient MenuContainer parent;
  46.  
  47.     /**
  48.      * The AppContext of the MenuComponent.  This is set in the constructor
  49.      * and never changes.
  50.      */
  51.     transient AppContext appContext;
  52.  
  53.     /**
  54.      * The Menu Components Font.
  55.      * This value can be null at which point a default will be used.
  56.      *
  57.      * @serial
  58.      * @see setFont()
  59.      * @see getFont()
  60.      */
  61.     Font font;
  62.     /**
  63.      * The Menu Components name.
  64.      * @serial
  65.      * @see getName()
  66.      * @see setName()
  67.      */
  68.     private String name;
  69.     /**
  70.      * A variable to indicate whether a name is explicitly set.
  71.      * If it is true the name will be set explicitly.
  72.      * @serial
  73.      * @see setName()
  74.      */
  75.     private boolean nameExplicitlySet = false;
  76.     /**
  77.      * @serial
  78.      * @see dispatchEvent()
  79.      */
  80.     boolean newEventsOnly = false;
  81.  
  82.     /*
  83.      * Internal constants for serialization 
  84.      */
  85.     final static String actionListenerK = Component.actionListenerK;
  86.     final static String itemListenerK = Component.itemListenerK;
  87.  
  88.     /*
  89.      * JDK 1.1 serialVersionUID 
  90.      */
  91.     private static final long serialVersionUID = -4536902356223894379L;
  92.  
  93.     /**
  94.      * Constructor for MenuComponent.
  95.      */
  96.     public MenuComponent() {
  97.     appContext = AppContext.getAppContext();
  98.     SunToolkit.insertTargetMapping(this, appContext);
  99.     }
  100.  
  101.     /**
  102.      * Construct a name for this MenuComponent.  Called by getName() when
  103.      * the name is null.
  104.      */
  105.     String constructComponentName() {
  106.         return null; // For strict compliance with prior JDKs, a MenuComponent
  107.                      // that doesn't set its name should return null from
  108.                      // getName()
  109.     }
  110.  
  111.     /**
  112.      * Gets the name of the menu component.
  113.      * @return        the name of the menu component.
  114.      * @see           java.awt.MenuComponent#setName(java.lang.String)
  115.      * @since         JDK1.1
  116.      */
  117.     public String getName() {
  118.         if (name == null && !nameExplicitlySet) {
  119.             synchronized(this) {
  120.                 if (name == null && !nameExplicitlySet)
  121.                     name = constructComponentName();
  122.             }
  123.         }
  124.         return name;
  125.     }
  126.  
  127.     /**
  128.      * Sets the name of the component to the specified string.
  129.      * @param         name    the name of the menu component.
  130.      * @see           java.awt.MenuComponent#getName
  131.      * @since         JDK1.1
  132.      */
  133.     public void setName(String name) {
  134.         synchronized(this) {
  135.             this.name = name;
  136.             nameExplicitlySet = true;
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * Returns the parent container for this menu component.
  142.      * @return    the menu component containing this menu component, 
  143.      *                 or <code>null</code> if this menu component 
  144.      *                 is the outermost component, the menu bar itself.
  145.      */
  146.     public MenuContainer getParent() {
  147.     return getParent_NoClientCode();
  148.     }
  149.     // NOTE: This method may be called by privileged threads.
  150.     //       This functionality is implemented in a package-private method 
  151.     //       to insure that it cannot be overridden by client subclasses. 
  152.     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  153.     final MenuContainer getParent_NoClientCode() {
  154.     return parent;
  155.     }
  156.  
  157.     /**
  158.      * @deprecated As of JDK version 1.1,
  159.      * programs should not directly manipulate peers.
  160.      */
  161.     public MenuComponentPeer getPeer() {
  162.     return peer;
  163.     }
  164.  
  165.     /**
  166.      * Gets the font used for this menu component.
  167.      * @return   the font used in this menu component, if there is one; 
  168.      *                  <code>null</code> otherwise.
  169.      * @see     java.awt.MenuComponent#setFont
  170.      */
  171.     public Font getFont() {
  172.     Font font = this.font;
  173.     if (font != null) {
  174.         return font;
  175.     }
  176.     MenuContainer parent = this.parent;
  177.     if (parent != null) {
  178.         return parent.getFont();
  179.     }
  180.     return null;
  181.     }
  182.  
  183.     // NOTE: This method may be called by privileged threads.
  184.     //       This functionality is implemented in a package-private method 
  185.     //       to insure that it cannot be overridden by client subclasses. 
  186.     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  187.     final Font getFont_NoClientCode() {
  188.     Font font = this.font;
  189.     if (font != null) {
  190.         return font;
  191.     }
  192.  
  193.     // The MenuContainer interface does not have getFont_NoClientCode()
  194.     // and it cannot, because it must be package-private. Because of
  195.     // this, we must manually cast classes that implement 
  196.     // MenuContainer.
  197.     Object parent = this.parent;
  198.     if (parent != null) {
  199.         if (parent instanceof Component) {
  200.         font = ((Component)parent).getFont_NoClientCode();
  201.         } else if (parent instanceof MenuComponent) {
  202.         font = ((MenuComponent)parent).getFont_NoClientCode();
  203.         }
  204.     }
  205.     return font;
  206.     } // getFont_NoClientCode()
  207.  
  208.  
  209.     /**
  210.      * Sets the font to be used for this menu component to the specified 
  211.      * font. This font is also used by all subcomponents of this menu 
  212.      * component, unless those subcomponents specify a different font. 
  213.      * @param     f   the font to be set.
  214.      * @see       java.awt.MenuComponent#getFont
  215.      */
  216.     public void setFont(Font f) {
  217.     font = f;
  218.     }
  219.  
  220.     /**
  221.      * Removes the menu component's peer.  The peer allows us to modify the
  222.      * appearance of the menu component without changing the functionality of
  223.      * the menu component.
  224.      */
  225.     public void removeNotify() {
  226.         synchronized (getTreeLock()) {
  227.         MenuComponentPeer p = (MenuComponentPeer)this.peer;
  228.         if (p != null) {
  229.             Toolkit.getEventQueue().removeSourceEvents(this);
  230.         this.peer = null;
  231.         p.dispose();
  232.         }
  233.     }
  234.     }
  235.  
  236.     /**
  237.      * Posts the specified event to the menu.
  238.      * This method is part of the Java 1.0 event system
  239.      * and it is maintained only for backwards compatibility.
  240.      * Its use is discouraged, and it may not be supported
  241.      * in the future.
  242.      * @param evt the event which is to take place
  243.      * @deprecated As of JDK version 1.1,
  244.      * replaced by <code>dispatchEvent(AWTEvent)</code>.
  245.      */
  246.     public boolean postEvent(Event evt) {
  247.     MenuContainer parent = this.parent;
  248.     if (parent != null) {
  249.         parent.postEvent(evt);
  250.     }
  251.     return false;
  252.     }
  253.  
  254.     /*
  255.      * Delivers an event to this component or one of its sub components.
  256.      * @param e the event
  257.      */
  258.     public final void dispatchEvent(AWTEvent e) {
  259.         dispatchEventImpl(e);
  260.     }
  261.  
  262.     void dispatchEventImpl(AWTEvent e) {
  263.         Toolkit.getDefaultToolkit().notifyAWTEventListeners(e);
  264.  
  265.         if (newEventsOnly || 
  266.             (parent != null && parent instanceof MenuComponent &&
  267.              ((MenuComponent)parent).newEventsOnly)) {
  268.             if (eventEnabled(e)) {
  269.                 processEvent(e);
  270.             } else if (e instanceof ActionEvent && parent != null) {
  271.                 ((MenuComponent)parent).dispatchEvent(new ActionEvent(parent, 
  272.                                          e.getID(),
  273.                                          ((ActionEvent)e).getActionCommand()));
  274.             }
  275.                 
  276.         } else { // backward compatibility
  277.             Event olde = e.convertToOld();
  278.             if (olde != null) {
  279.                 postEvent(olde);
  280.             }
  281.         }
  282.     }
  283.  
  284.     // REMIND: remove when filtering is done at lower level
  285.     boolean eventEnabled(AWTEvent e) {
  286.         return false;
  287.     }        
  288.     /** 
  289.      * Processes events occurring on this menu component.  
  290.      *
  291.      * @param e the event
  292.      * @since JDK1.1
  293.      */   
  294.     protected void processEvent(AWTEvent e) {
  295.     }
  296.  
  297.     /**
  298.      * Returns the parameter string representing the state of this  
  299.      * menu component. This string is useful for debugging. 
  300.      * @return     the parameter string of this menu component.
  301.      */
  302.     protected String paramString() {
  303.         String thisName = getName();
  304.         return (thisName != null? thisName : "");
  305.     }
  306.  
  307.     /**
  308.      * Returns a representation of this menu component as a string. 
  309.      * @return  a string representation of this menu component.
  310.      */
  311.     public String toString() {
  312.     return getClass().getName() + "[" + paramString() + "]";
  313.     }
  314.  
  315.     /**
  316.      * Gets this component's locking object (the object that owns the thread 
  317.      * sychronization monitor) for AWT component-tree and layout
  318.      * operations.
  319.      * @return This component's locking object.
  320.      */
  321.     protected final Object getTreeLock() {
  322.         return Component.LOCK;
  323.     }
  324.  
  325.     private void readObject(ObjectInputStream s)
  326.         throws ClassNotFoundException, IOException
  327.     {
  328.         s.defaultReadObject();
  329.  
  330.     appContext = AppContext.getAppContext();
  331.     SunToolkit.insertTargetMapping(this, appContext);
  332.     }
  333.  
  334.     /**
  335.      * Initialize JNI field and method IDs
  336.      */
  337.     private static native void initIDs();
  338. }
  339.